package base.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lxb on 2016/7/21.
 */
public class HttpClient {
    /**
     * post方式提交表单
     */
    public void postForm() throws IOException {
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost
        HttpPost httppost = new HttpPost("http://www.jtsyjc.net/SearchOrgan.aspx");
        // 创建参数队列
        List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
        formparams.add(new BasicNameValuePair("__VIEWSTATE", "fhivP2NLg3+xWpd8Mp+LTOWEJhCIMP0wEqGKv2ddgS4x1lp6wu3NewYsvBCAFdI/17J1X4fJD9J11Yl/4l5y/XirPIrDXbaP3F9m+Cbx6sJZzbD9JSigWVcU8eXScbXEhwOJAZcQ+Ehu4eZUVwLAS/Hr54wOL7VO/AEKjaflrS/N0HQXsnhBjH7KW80OQf6C2rwVZn7Dn7wwOm9J1LZvbuTB/h8zbtXDFMqyNtqdFfAGJ+o8Rh2zyfss44RxJRBlCNEVd1QkZqyTEeQShPtjiZLxAdcs1Z4Vp1FsRkkcR27GlQIUWJAOYwWbYD+Hw3+4fMGlb+ElZcML27drOtVdlz/w/MutQoVSA7XChF+XPFlEonnC32YxOaTj1OPtzk5eTarSOICfizYPCdj8pgyu50H6kj8KHLhrGThAoYakrb0CU3jJgkaDLNi9PbL4nKxxajfJ/jZ6UltNpubcffKILsLbJ0lKKM30N0Ys+VqxcG9lfITIYEBxeecOr/Fz26QaKLifG0JVzwOrsVFhAVRxbNoGOYK/YlkBmmuTxylQn6bScRvx5uU3ZJwrH0FifZnjZcjkdPBgqI4mQe+1QZd7n91Cl4luvAThHfGsNNQt4Uv5unLVnZVTfyO8fmXS/PVB2fHWINBJLOzMmchZkrUIsYL/jjZFYlL5Jlw+qTzr7Pq51gzu/ZOu94Gy2hiD/erEOFkVlVD9D4nbXJ2OouwS8TC91Glf4I6WC8svlX1idMTC+m+2l1gqC39PA+nSHda67M0aoG4U95xsjvKZ6n83ozGJRUS2OMNrQVON2PfeLn49wsiDI6x87ApCVeHbiiPof4daiipMrQqyUJwHaYgXtpxmFZba9vH0lXk3cDeZxAthd2IXz8DZ9QFR68x/V0MZ1J+/zuWg632VPmYVreZ3ANJeeuDQNyeQQ88LZGGIr/1BhawfpAKIzz3smZEHgAbOxcusRTJcbUFntB+2SjkqesXk1fvI1aTXRTMWUZLxFa78KkzurL44Xik1lqtersGAZ6m4acaOmjbDD1RdfGzgx7VqNiFYpP2qebGPpWWX7B4Tkq92+uGePud+JizJdRZcxkoJUKwcUS3YKQulxri+sjk79uYePnV/Yizr0SNGslG3UN3m9a8z2hINQ2RFtenxLDa60Mh2D4LxXwTkTDgLRaZXqIrw9SO1G6zhwfl0Fmgr8Cwt0U0id3zepQQO7fVVn+J49MBohrO7FFZYwiqYe4k+LX8VW23Bc+40FKVvpfHR2kKymsb+GNf4At+dLS27RVdYnH+0VkR3LvzCEZitArZY2Z5yBe0Th4R1D3hoFcYe/R8DS5GXyMKxaADWRJuoCzJ1tAtxRDXnIq6t/VP45oGeNciMw8iFzU7NDcPSxDIcpcq9VnShwJkob6o7XJ3H"));
        formparams.add(new BasicNameValuePair("__EVENTVALIDATION", "qAPcXzExNFUilhz35HqyfUyCFODqhw214VrkkI44qNLEmd8gy1s7eGF4DWz5eVQfVL5lmDASpD+5eTYDF1lU96QvFBY1wFbho44f73WTj1St34SoLfvK+lp1LaGC2jIUtOJ9yLYFT3WJX4aGHOEem5XTXTjDtaQpvzLZZRcnleoV/WyZP00jBCJ7u9+ou/UwVUvbzT4pVIcvda+vrcYQKWob3l+KYMbGzr2gqm7a2sSvmJ9HexUaVYg+xGR3eRaDapcfdCsYzYyZ+f6L1eoyvgOaHe2AqR82d3Xfnys4nP/3y7rfAXcl/rpMjO/gCp9ddCo9qfdhtby63hvFwRiJV10g2Hw6j+Lr2kJg0rUF1WpYRJq3/pi/8yt+hI7/5Zoq"));
        formparams.add(new BasicNameValuePair("_ctl0:ContentPlaceHolder1:DropDownList版本1", "beea29e2-4fc6-4b62-b887-587eca85c8fe"));
        formparams.add(new BasicNameValuePair("_ctl0:ContentPlaceHolder1:drop省", "上海市"));
        formparams.add(new BasicNameValuePair("_ctl0:ContentPlaceHolder1:DropDownList专业1", "9d974b4f-b628-446b-bf8b-90eb579bda16"));
        formparams.add(new BasicNameValuePair("_ctl0:ContentPlaceHolder1:drop资质等级", "2e0c51ed-ae29-4558-afce-a28245cfa84c"));
        formparams.add(new BasicNameValuePair("_ctl0:ContentPlaceHolder1:imgbt1.x", "31"));
        formparams.add(new BasicNameValuePair("_ctl0:ContentPlaceHolder1:imgbt1.y", "11"));
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    System.out.println("--------------------------------------");
                    System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
                    System.out.println("--------------------------------------");
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        HttpClient httpClient=new HttpClient();
        httpClient.postForm();
    }
}
